package com.me.servlet;

import com.me.controller.Controller;
import com.me.controller.UserController;
import com.me.utils.IOUtility;
import com.me.utils.ServUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Controller controller = new UserController(request,response);
		
		String tempState = ServUtil.parseState(request);
		
		String state = IOUtility.getProperty(tempState);
		String page = null;
		
		if (state == null || state == ""){
			state = ServUtil.getRegState(tempState,IOUtility.getProperty("u.regex"));
		}
		
		if (state != null && state != ""){
			page = controller.excute(state);
		}
		
		page = page == null ? "/error.jsp" : page;
		
		
		if (page.startsWith(".")){
			response.sendRedirect(page.substring(1));
		}else{
			request.getRequestDispatcher(page).forward(request,response);
		}
	}
}
